﻿using System;
using Assets.Scripts.GtsScripts;
using RootMotion.FinalIK;
using UnityEngine;

// Token: 0x020002C8 RID: 712
public class HandIK
{
	// Token: 0x06001095 RID: 4245 RVA: 0x00079AE8 File Offset: 0x00077CE8
	public HandIK(FullBodyBipedIK ik, Giantess giantess)
	{
		this.ik = ik;
		this.giantess = giantess;
		this.hand = ik.references.rightHand;
		if (giantess.ik == null)
		{
			Debug.LogError("No IK");
		}
		if (giantess.ik.rightHandEffector == null)
		{
			Debug.LogError("No hand ik effector");
		}
		this.handEffector = giantess.ik.rightHandEffector;
		this.targetWeight = 0f;
		ik.solver.leftArmMapping.weight = 0f;
		this.CurrentState = new HandIK.State(this.Idle);
	}

	// Token: 0x06001096 RID: 4246 RVA: 0x00079BA0 File Offset: 0x00077DA0
	public void GrabTarget(EntityBase entity)
	{
		if (this.giantess.Height < entity.Height * 5f)
		{
			this.giantess.animationManager.PlayAnimation("No 2", false, false);
			if (GlobalPreferences.ScriptAuxLogging.value)
			{
				Debug.Log("Target is too large to grab");
			}
			return;
		}
		this.target = entity;
		this.CurrentState = new HandIK.State(this.Grab);
	}

	// Token: 0x06001097 RID: 4247 RVA: 0x00079C13 File Offset: 0x00077E13
	public void CancelGrab()
	{
		this.target = null;
		this.CurrentState = new HandIK.State(this.Return);
	}

	// Token: 0x06001098 RID: 4248 RVA: 0x00079C2E File Offset: 0x00077E2E
	public void ReleaseTarget()
	{
		if (this.target != null)
		{
			this.target.transform.parent = null;
			this.target.Unlock();
		}
	}

	// Token: 0x06001099 RID: 4249 RVA: 0x00079C5D File Offset: 0x00077E5D
	public bool GrabCompleted()
	{
		return !this.IsActive();
	}

	// Token: 0x0600109A RID: 4250 RVA: 0x00079C68 File Offset: 0x00077E68
	public void Update()
	{
		this.CurrentState();
		this.UpdateEffector();
	}

	// Token: 0x0600109B RID: 4251 RVA: 0x00079C7B File Offset: 0x00077E7B
	public bool IsActive()
	{
		return this.CurrentState != new HandIK.State(this.Idle);
	}

	// Token: 0x0600109C RID: 4252 RVA: 0x00079C94 File Offset: 0x00077E94
	private void UpdateEffector()
	{
		Vector3 position = this.handEffector.position;
		Vector3 position2 = Vector3.MoveTowards(position, this.targetPos, Time.fixedDeltaTime * 25f);
		float positionWeight = this.handEffector.positionWeight;
		float num = Mathf.Lerp(positionWeight, this.targetWeight, Time.fixedDeltaTime);
		this.handEffector.position = position2;
		this.handEffector.positionWeight = num;
		this.ik.solver.rightArmMapping.weight = num;
	}

	// Token: 0x0600109D RID: 4253 RVA: 0x00079D11 File Offset: 0x00077F11
	private void Idle()
	{
		this.targetWeight = 0f;
		this.handEffector.position = this.hand.position;
	}

	// Token: 0x0600109E RID: 4254 RVA: 0x00079D34 File Offset: 0x00077F34
	private void Grab()
	{
		if (this.target == null || this.target.isDead)
		{
			this.CurrentState = new HandIK.State(this.Return);
			return;
		}
		float num = 800f * this.giantess.Scale;
		this.targetWeight = 1f;
		this.targetPos = this.target.transform.position;
		float sqrMagnitude = (this.hand.position - this.targetPos).sqrMagnitude;
		float num2 = this.giantess.AccurateScale * 0.1f;
		float num3 = num2 * num2;
		if (sqrMagnitude < num3)
		{
			this.target.transform.SetParent(this.hand);
			this.target.Lock();
			this.CurrentState = new HandIK.State(this.MoveUp);
			this.grabTime = Time.time;
			return;
		}
		Vector3 a = new Vector3(this.giantess.transform.position.x, Mathf.Clamp(this.targetPos.y, this.giantess.transform.position.y, this.giantess.transform.position.y + this.giantess.meshHeight * (this.giantess.Scale / 2f)), this.giantess.transform.position.z);
		float sqrMagnitude2 = (a - this.targetPos).sqrMagnitude;
		if (sqrMagnitude2 > num * num)
		{
			this.CurrentState = new HandIK.State(this.Return);
			return;
		}
	}

	// Token: 0x0600109F RID: 4255 RVA: 0x00079EF4 File Offset: 0x000780F4
	private void MoveUp()
	{
		if (this.target == null || this.target.isDead)
		{
			this.CurrentState = new HandIK.State(this.Return);
			return;
		}
		this.targetPos = this.giantess.transform.TransformPoint(new Vector3(100f, 1400f, 300f));
		if (Time.time > this.grabTime + this.holdTime || !this.target.locked)
		{
			this.CurrentState = new HandIK.State(this.Return);
		}
	}

	// Token: 0x060010A0 RID: 4256 RVA: 0x00079F98 File Offset: 0x00078198
	private void Return()
	{
		this.targetWeight = 0f;
		this.targetPos = this.hand.transform.position;
		if (this.handEffector.positionWeight < 0.05f)
		{
			this.CurrentState = new HandIK.State(this.Idle);
		}
	}

	// Token: 0x0400155F RID: 5471
	private FullBodyBipedIK ik;

	// Token: 0x04001560 RID: 5472
	private Giantess giantess;

	// Token: 0x04001561 RID: 5473
	private EntityBase target;

	// Token: 0x04001562 RID: 5474
	private Transform hand;

	// Token: 0x04001563 RID: 5475
	private IKBone handEffector;

	// Token: 0x04001564 RID: 5476
	private float targetWeight;

	// Token: 0x04001565 RID: 5477
	private Vector3 targetPos;

	// Token: 0x04001566 RID: 5478
	private HandIK.State CurrentState;

	// Token: 0x04001567 RID: 5479
	private float grabTime;

	// Token: 0x04001568 RID: 5480
	private float holdTime = 20f;

	// Token: 0x020002C9 RID: 713
	// (Invoke) Token: 0x060010A2 RID: 4258
	private delegate void State();
}
